package com.sgu6.sgu6_machine.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.sgu6.sgu6_machine.event.SignalStrengthsEvent;

import org.greenrobot.eventbus.EventBus;

public class PhoneSignalStateS extends Service {

    private static final String TAG = PhoneSignalStateS.class.getSimpleName();
    public static final int NETWORK_TYPE_WIFI = 0;
    public static final int NETWORK_TYPE_4G = 1;
    public static final int NETWORK_TYPE_3G = 2;
    public static final int NETWORK_TYPE_NONE = 3;
    private TelephonyManager tel;
    private PhoneSignalStateListener signalListener = new PhoneSignalStateListener();


    @Override
    public void onCreate() {
        super.onCreate();
        tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand: ");
        tel.listen(signalListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
        return START_NOT_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    private class PhoneSignalStateListener extends PhoneStateListener {
        /* Get the Signal strength from the provider, each tiome there is an update  从得到的信号强度,每个tiome供应商有更新*/
        @Override
        public void onSignalStrengthsChanged(SignalStrength signalStrength) {
            super.onSignalStrengthsChanged(signalStrength);
            //获取网络信号强度
            //获取0-4的5种信号级别，越大信号越好,但是api23开始才能用
            //int level = signalStrength.getLevel();
            int gsmSignalStrength = signalStrength.getGsmSignalStrength();

            //获取网络类型
            int netWorkType = getNetWorkType();
            switch (netWorkType) {
                case NETWORK_TYPE_WIFI:
                    EventBus.getDefault().post(new SignalStrengthsEvent(netWorkType, gsmSignalStrength, true));
                    Log.i(TAG, "onSignalStrengthsChanged: 当前网络为wifi,信号强度为：" + gsmSignalStrength);
                    break;
                case NETWORK_TYPE_3G:
                    EventBus.getDefault().post(new SignalStrengthsEvent(netWorkType, gsmSignalStrength, false));
                    Log.i(TAG, "onSignalStrengthsChanged: 当前网络为3G移动网络,信号强度为：" + gsmSignalStrength);
                    break;
                case NETWORK_TYPE_4G:
                    EventBus.getDefault().post(new SignalStrengthsEvent(netWorkType, gsmSignalStrength, true));
                    Log.i(TAG, "onSignalStrengthsChanged: 当前网络为4G移动网络,信号强度为：" + gsmSignalStrength);
                    break;
                case NETWORK_TYPE_NONE:
                    EventBus.getDefault().post(new SignalStrengthsEvent(netWorkType, gsmSignalStrength, false));

                    Log.i(TAG, "onSignalStrengthsChanged: 当前没有网络,信号强度为：" + gsmSignalStrength);
                    break;
                case -1:
                    Log.i(TAG, "onSignalStrengthsChanged: 信号强度为：" + gsmSignalStrength);
                    break;
            }
        }
    }


    /**
     * 获取网络类型
     *
     * @return
     */
    private int getNetWorkType() {
        int mNetWorkType = -1;
        ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            String type = networkInfo.getTypeName();
            if (type.equalsIgnoreCase("WIFI")) {
                mNetWorkType = NETWORK_TYPE_WIFI;
            } else if (type.equalsIgnoreCase("MOBILE")) {
                return isFastMobileNetwork() ? NETWORK_TYPE_4G : NETWORK_TYPE_3G;
            }
        } else {
            mNetWorkType = NETWORK_TYPE_NONE;//没有网络
        }
        return mNetWorkType;
    }

    /**
     * 判断网络类型
     */
    private boolean isFastMobileNetwork() {
        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        //这里只简单区分两种类型网络，认为4G网络为快速，但最终还需要参考信号值
        return telephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_LTE;
    }
}
